define([
	"jquery",
	"baseview",
	"text!static/pages/super/tpl/manageredit.tpl.html",
	"common"
], function(
	$,
	BaseView,
	template,
	Common
) {
	var self;
	var v_check = false;
	var view = BaseView.extend({
		setTemplate: function() {
			this.el.html("").html(template);
			Common.breadcrumb();
		},
		fireEvents: function() {
			self = this;
			var id = Common.store.get("temp_manageredit_id");
			if(id) {
				$("#managercode").attr("disabled", true);
				var pm = {
					"id": id,
					"ucode": Common.userContext().ucode
				};
				Common.wrapajax({
					url: Common.baseUrl + "/manager/byid",
					contentType: "application/x-www-form-urlencoded",
					type: "post",
					dataType: "json",
					data: pm,
					success: function(data) {
						if(data.managercode) {
							$("#managercode").val(data.managercode);
							$("#managername").val(data.managername);
							$("#createtime").val(data.createtime);
							if(data.isenable == 1) {
								$("#isenable").attr("checked", true);
								//$("#isenable").prop("checked",true);
							} else {
								$("#isenable").attr("checked", false);
								//$("#isenable").prop("checked",false);
							}
						} else {
							$("#managername").attr("disabled", true);
							$("#isenable").attr("disabled", true);
							Common.toastr().error("没有查询到该管理员信息！");
						}
					}
				});
			} else {
				self.viewmodel.managerInfo.createEmptyRow();
				self.viewmodel.managerInfo.setRowSelect(0);
			}

			$("#createtime").attr("disabled", true);
		},
		events: {
			submit: function() {
				var mcode = $("#managercode").val() || '';
				var mname = $("#managername").val() || '';
				var isenable = v_check == true ? 1 : 0;
				if(mcode == '' || mname == '') {
					Common.toast("管理员编码或名称不允许为空!");
					return;
				}
				var pm = {
					"id": Common.store.get("temp_manageredit_id") || '',
					"ucode": Common.userContext().ucode,
					"managercode": mcode,
					"managername": mname,
					"isenable": isenable
				};

				Common.wrapajax({
					url: Common.baseUrl + "/manager/add",
					contentType: "application/x-www-form-urlencoded",
					type: "post",
					dataType: "json",
					data: pm,
					success: function(data) {
						//self.viewmodel.managerInfo.trigger("cpu_save_action", data);
						if(data.code == "success") {
							Common.toastr().success("保存成功！")
							window.location.hash = "#/pages/super/manager";
						} else {
							Common.toastr().error("保存失败！" + data.errormsg);
						}

					}
				});
			}
		},
		viewmodel: {
			el: "#manageredit",
			managerInfo: new $.DataTable({
				params: {},
				meta: {
					'id': {},
					'managercode': {},
					'managername': {},
					'isenable': {},
					'createtime': {}
				},
				pageSize: 10
			})
		},
		bindEvents: function() {
			
			$("#isenable").change(function(){
				//alert(this.checked);
				v_check = this.checked;
			});

		}

	});
	return view;
})